package com.yylending.las.personal.bandcard;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.Loader;
import android.text.TextUtils;
import android.view.KeyEvent;

import com.yyfax.infrastructure.eventbus.EventAgent;
import com.yyfax.infrastructure.mvp.Presenter;
import com.yylending.las.Constants;
import com.yylending.las.R;
import com.yylending.las.base.BaseActivity;
import com.yylending.las.StatIDSet;
import com.yyfax.infrastructure.statistics.StatService;
import com.yylending.las.common.widgets.CommonDialog;
import com.yylending.las.lending.quota.myquota.MyQuotaModel;
import com.yylending.las.personal.bandcard.cardinfo.CardInfoFragment;

import java.util.ArrayList;

public class BankCardActivity extends BaseActivity {
    private final static String TAG = "BankCardActivity";

    public static final String TAG_MANAGE_BANK_CARD_FRAGMENT = "1";
    public static final String TAG_ADD_CARD_FRAGMENT = "2";
    public static final String TAG_CARD_INFO_FRAGMENT = "3";

    private static final String KEY_INTENT_HAS_BIND_BANK_CARD_LIST = "key_intent_has_bind_bank_card_list";
    public static final String KEY_INTENT_APPLY_STATUS = "intent_apply_status";

    public static final String KEY_INTENT_ORIGIN_FROM_TO_BIND_CARD = "key_intent_origin_from_to_bind_card";

    public final static int LOADER_ID_GET_REAL_NAME_VERIFY = 204;
    public final static int LOADER_ID_BIND_CARD = 205;
    public final static int LOADER_ID_GET_CARD_BIN = 206;

    //已绑定银行卡信息
    private ArrayList<BoundBankCards.BankInfo> mBankInfo = new ArrayList<>();
    private String mCurrentPage = TAG_MANAGE_BANK_CARD_FRAGMENT;
    private String mOriginFrom = "";

    public static void start(Activity aty, ArrayList<BoundBankCards.BankInfo> bankInfos, MyQuotaModel.ApplyStatus applyStatus, String originFrom) {
        Intent intent = new Intent(aty, BankCardActivity.class);
        if (bankInfos != null) {
            intent.putParcelableArrayListExtra(KEY_INTENT_HAS_BIND_BANK_CARD_LIST, bankInfos);
        }
        if (applyStatus != null) {
            intent.putExtra(KEY_INTENT_APPLY_STATUS, applyStatus);
        }
        intent.putExtra(KEY_INTENT_ORIGIN_FROM_TO_BIND_CARD, originFrom);
        aty.startActivity(intent);
    }

    @Override
    public Loader<Presenter> onCreateLoader(int id, Bundle bundle) {
        return null;
    }

    @Override
    public void onLoadFinished(Loader<Presenter> loader, Presenter data, int placeholder) {
    }

    @Override
    protected void onStart() {
        super.onStart();
        EventAgent.register(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        EventAgent.unregister(this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setTitleBarColor(R.color.color_top_bg_blue);
        setContentView(R.layout.activity_bank_card);
        setTitle(R.string.title_activity_bind_bank_card);
        ArrayList<BoundBankCards.BankInfo> bankInfoList = getIntent().getParcelableArrayListExtra(KEY_INTENT_HAS_BIND_BANK_CARD_LIST);
        mOriginFrom = getIntent().getStringExtra(KEY_INTENT_ORIGIN_FROM_TO_BIND_CARD);

        if (bankInfoList != null){
            mBankInfo.addAll(bankInfoList);
        }
        if (mBankInfo != null && mBankInfo.size() > 0) {
            showFragment(TAG_CARD_INFO_FRAGMENT);
        } else {
            if(TextUtils.equals(Constants.KEY_GOTO_BIND_CARD_FROM_HAVE_QUOTA_FRAGMENT, mOriginFrom)){
				showFragment(TAG_ADD_CARD_FRAGMENT);
			}else{
				showFragment(TAG_MANAGE_BANK_CARD_FRAGMENT);
			}
        }
    }

    public void showFragment(String page) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment mManageBankCardFragment = fm.findFragmentByTag(TAG_MANAGE_BANK_CARD_FRAGMENT);
        Fragment mAddCardFragment = fm.findFragmentByTag(TAG_ADD_CARD_FRAGMENT);
        Fragment mCardInfoFragment = fm.findFragmentByTag(TAG_CARD_INFO_FRAGMENT);

        if (mManageBankCardFragment != null) {
            ft.hide(mManageBankCardFragment);
        }
        if (mAddCardFragment != null) {
            // 清除已填信息.
            ((AddCardFragment)mAddCardFragment).clearText();
            ft.hide(mAddCardFragment);
        }
        if (mCardInfoFragment != null) {
            ft.hide(mCardInfoFragment);
        }

        mCurrentPage = page;
        switch (page) {
            case TAG_MANAGE_BANK_CARD_FRAGMENT:
                if (mManageBankCardFragment == null) {
                    mManageBankCardFragment = ManageBankCardFragment.newInstance();
                    ft.add(R.id.band_card_container, mManageBankCardFragment, TAG_MANAGE_BANK_CARD_FRAGMENT);
                } else {
                    ft.show(mManageBankCardFragment);
                }
                break;
            case TAG_ADD_CARD_FRAGMENT:
                if (mAddCardFragment == null) {
                    mAddCardFragment = AddCardFragment.newInstance(mOriginFrom);
                    ft.add(R.id.band_card_container, mAddCardFragment, TAG_ADD_CARD_FRAGMENT);
                } else {
                    ft.show(mAddCardFragment);
                }
                break;
            case TAG_CARD_INFO_FRAGMENT:
				StatService.onEvent(this, StatIDSet.EVENT_ID_MANAGEMENT_OF_BANK_CARD);
				if (mCardInfoFragment == null) {
                    mCardInfoFragment = CardInfoFragment.newInstance(mBankInfo);
                    ft.add(R.id.band_card_container, mCardInfoFragment, TAG_CARD_INFO_FRAGMENT);
                } else {
                    ft.show(mCardInfoFragment);
                }
                break;
            default:
                break;
        }

        ft.commitAllowingStateLoss();
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            doForwardOrBack(true);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }


    @Override
    protected void onBackBtnPressed() {
        doForwardOrBack(true);
    }

    public void doForwardOrBack(boolean isBack) {
        if (isBack) {
            switch (mCurrentPage) {
                case TAG_MANAGE_BANK_CARD_FRAGMENT:
                    finish();
                    break;
                case TAG_ADD_CARD_FRAGMENT:
                    judgePage();
                    break;
                case TAG_CARD_INFO_FRAGMENT:
                    finish();
                    break;
                default:
                    break;
            }
        } else {
            switch (mCurrentPage) {
                case TAG_MANAGE_BANK_CARD_FRAGMENT:
                    showFragment(TAG_ADD_CARD_FRAGMENT);
                    break;
                case TAG_ADD_CARD_FRAGMENT:
                    showFragment(TAG_CARD_INFO_FRAGMENT);
                    break;
                default:
                    break;
            }
        }
    }

    private void judgePage() {
		FragmentManager fm = getSupportFragmentManager();
		Fragment mAddCardFragment = fm.findFragmentByTag(TAG_ADD_CARD_FRAGMENT);
		boolean isGiveUp = ((AddCardFragment)mAddCardFragment).giveUpBind();
		if(isGiveUp){
			showTwoBtnDialog(getString(R.string.give_up_bind_bank), getString(R.string.give_up_bind), getString(R.string.cancel), bindCardDialogListener, true);
		}else{
			if(TextUtils.equals(Constants.KEY_GOTO_BIND_CARD_FROM_HAVE_QUOTA_FRAGMENT, mOriginFrom)){
				finish();
			}else{
				showFragment(TAG_MANAGE_BANK_CARD_FRAGMENT);
			}
		}
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private CommonDialog.ButtonClickListener bindCardDialogListener = new CommonDialog.ButtonClickListener(){
		@Override
		public void onCancel(CommonDialog dialog) {
			dialog.dismiss();
		}

		@Override
		public void onConfirm(CommonDialog dialog) {
			dialog.dismiss();
			if(TextUtils.equals(Constants.KEY_GOTO_BIND_CARD_FROM_HAVE_QUOTA_FRAGMENT, mOriginFrom)){
				finish();
			}else{
				showFragment(TAG_MANAGE_BANK_CARD_FRAGMENT);
			}
		}
	};
}
